#include "GGameManager.h"

#include "GCore.h"
#include "GInputContext.h"

#include "GTypes.h"
#include "GError.h"

namespace GEngine
{
	GGameManager::GGameManager()
	{
		ActiveInputMap = NULL;
		GlobalInputMap = NULL;

		ActiveInputMapDefault = NULL;	
		GlobalInputMapDefault = NULL;
	}

	GGameManager::~GGameManager()
	{
	}

	void GGameManager::ResetActiveInputMap()
	{
		SetActiveInputMap(ActiveInputMapDefault);
	}

	void GGameManager::ResetGlobalInputMap()
	{
		SetGlobalInputMap(GlobalInputMapDefault);
	}

	void GGameManager::SetActiveInputMap(GInputMap* Map)
	{
		// Mark maps as in/active
		ActiveInputMap->SetActive(false);
		ActiveInputMap = Map;
		ActiveInputMap->SetActive(true);
	}

	GInputMap* GGameManager::GetActiveInputMap() const
	{
		return ActiveInputMap;
	}

	void GGameManager::SetGlobalInputMap(GInputMap* Map)
	{
		// Mark maps as in/active
		GlobalInputMap->SetActive(false);
		GlobalInputMap = Map;
		GlobalInputMap->SetActive(true);
	}

	GInputMap* GGameManager::GetGlobalInputMap() const
	{
		return GlobalInputMap;
	}

	bool GGameManager::Update(float ElapsedTime)
	{
		//! Update input maps

		//! Update scene here

		return true;
	}

	bool GGameManager::Initialize() OVERRIDE
	{
		if (!GInitializable::Initialize())
		{
			return false;
		}

		//// Create the module
		//const char* Name = ::mGModuleManager->GetImplementation("GInputModule", 0);
		//GUtil::GError::DebugCheck(!Name, "Cannot find a module which implements GInputModule.");
		//
		//GModule* Module = ::mGModuleManager->CreateModule(Name);
		//InputModule = static_cast<GInput::GInputModule*>(Module);
		//InputModule->GetDeviceCount();

		//Result = InputModule->Initialize();
		//GUtil::GError::DebugCheck(!Result, "Input module failed to initialize!");

		ActiveInputMapDefault =	new GInputMap;
		GlobalInputMapDefault = new GInputMap;

		ActiveInputMap = ActiveInputMapDefault;
		ActiveInputMap->SetActive(true);

		GlobalInputMap = GlobalInputMapDefault;
		GlobalInputMap->SetActive(true);

		return true;
	}

	bool GGameManager::InitializeLate() OVERRIDE
	{
		if (!GInitializable::InitializeLate())
		{
			return false;
		}

		return true;
	}
	
	void GGameManager::Uninitialize() OVERRIDE
	{		
		G_DELETE(GlobalInputMapDefault);
		G_DELETE(ActiveInputMapDefault);

		ActiveInputMap = NULL;
		GlobalInputMap = NULL;

		GInitializable::Uninitialize();
	}

	void GGameManager::UninitializeEarly() OVERRIDE
	{
		GInitializable::UninitializeEarly();
	}

}